package stud.mpgame.flash.scene;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.text.TextField;
import flash.utils.Timer;
import stud.mpgame.flash.IScene;
import flash.Lib;
import stud.mpgame.network.NetworkMonitor;
import flash.events.MouseEvent;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import stud.mpgame.network.Message;
import stud.mpgame.network.MessageDescription;
import stud.mpgame.flash.test.TestCommandDescription;
import stud.mpgame.flash.test.TestCommand;
import stud.mpgame.flash.test.TestSnapshotDescription;
import stud.mpgame.flash.test.TestSnapshot;
import stud.mpgame.snapshot.ISnapshot;
import stud.mpgame.snapshot.SnapshotManager;
import flash.events.TimerEvent;

/**
 * ...
 * @author GreK
 */

class GameScene extends Sprite, implements IScene {	
	
	public static inline var NAME = "GameScene";
	private var networkMonitor:NetworkMonitor;
	private var valueField:TextField;
	private var sendButton:TextField;	
	private var snapshotField:TextField;
	private var snapshotManager:SnapshotManager;
	
	public function new() {
		super();
        
		valueField = new TextField();
		valueField.border = true;
		valueField.type = TextFieldType.INPUT;
		valueField.width = 200;
		valueField.height = 18;
		valueField.text = "10";
		valueField.x = valueField.y = 10;
		
		addChild(valueField);
		
		snapshotField = new TextField();
		snapshotField.border = true;
		snapshotField.type = TextFieldType.DYNAMIC;
		snapshotField.width = 200;
		snapshotField.height = 18;
		snapshotField.text = "";
		snapshotField.x = 10;
		snapshotField.y = 34;
		
		addChild(snapshotField);
		
		sendButton = new TextField();
		sendButton.text = "Change";
		sendButton.selectable = false;
		sendButton.border = true;
		sendButton.type = TextFieldType.DYNAMIC;
		sendButton.width = 160;
		sendButton.height = 18;
		sendButton.x = 30;
		sendButton.y = 58;
		
		var format:TextFormat = new TextFormat();
		format.align = TextFormatAlign.CENTER;
		sendButton.setTextFormat(format);
		
		addChild(sendButton);
		
		sendButton.addEventListener(MouseEvent.CLICK, handleSendClick);
	}
	
	private function handleSendClick(e:MouseEvent) {
		var command:TestCommand = new TestCommand(Std.parseInt(valueField.text));
		var message:Message = new Message(networkMonitor, TestCommandDescription.getInstance());
		message.putUserObject(command);
		message.send();
	}
	
	//bydlo code start
	private var firstSnapshot:Bool;// = true;
	//bydlo code end
	
	private function testSnapshotRecieved(message:Message) {
		var testSnapshot:TestSnapshot = cast(message.getUserObject(TestSnapshot), TestSnapshot);
		//bydlo code start
		if (firstSnapshot) {
			//value == differenceTime(!)
			networkMonitor.setDifferenceTime(testSnapshot.getValue());
			snapshotManager = new SnapshotManager(25, new TestSnapshot(testSnapshot.getValue(), networkMonitor.nowServerTime()), networkMonitor);
		}
		//bydlo code end
		snapshotManager.addServerSnapshot(testSnapshot);
	}
	
	public function getName():String {
		return NAME;
	}
	
	public function init(data:Dynamic):Void {
		this.networkMonitor = data.networkMonitor;
		MessageDescription.registerDescription(TestSnapshotDescription.getInstance());
		networkMonitor.getMessageDispatcher().addMessageReceivedListenerByDescr(TestSnapshotDescription.getInstance(), testSnapshotRecieved);
		MessageDescription.registerDescription(TestCommandDescription.getInstance());
	}
	
	public function clear():Void {
		
	}
	
	public function update(deltaTime:Int):Void {
		var snapshot:ISnapshot = snapshotManager.update(deltaTime);
		snapshotField.text = "TIME: " + cast(snapshot, TestSnapshot).getTime() + " VALUE: " + cast(snapshot, TestSnapshot).getValue();
	}
	
	public function getContentRoot():DisplayObject {
		return this;
	}
}